//
//  CAEcgRecord.m
//  CareRingApp
//
//  Created by Linktop on 2024/4/7.
//

#import "CAEcgRecord.h"
#import "ConfigModel.h"
//#import <MJExtension/MJExtension.h>

const int ECG_MESURING_DURA_SECS_FOR_LOCAL = 30; //使用戒指算法的测量时间
extern const NSInteger MAIN_ECG_RES_ALL_DATA;

//NSString * const TABLE_NM_ECG = @"table_ecg";


@implementation CAEcgRecord

- (instancetype)initWithAccount:(NSString * )account
{
    self = [super init];
    if (self) {
        self.account = account;
        self.wearHand = LEFT_HAND; //默认
    }
    return self;
}

-(instancetype)init
{
    self = [super init];
    if (self) {
        self.wearHand = LEFT_HAND; //默认
    }
    return self;
}







-(void)setValuesWith:(CAEcgAlgorithmResults *) ecgRes
{
    self.arrhythmia = ecgRes.arrhythmia; // 心率检查结果

    self.ecgSq = ecgRes.ecgSq; // 信号质量

    // 心电图信号振幅很低，请确保接触面清洁，然后重试。
    self.low_amplitude = ecgRes.low_amplitude;

    //心电图信号有明显的噪音，请确保设备没有握得太紧，然后重试。
    self.significant_noise = ecgRes.significant_noise;

    //心电图信号似乎不稳定，请确保您保持静止，然后重试。
    self.unstable_signal = ecgRes.unstable_signal;

    //没有足够的数据，请确保遵循心电图记录指南，然后重试。
    self.not_enough_data = ecgRes.not_enough_data;

    self.rmssd = ecgRes.rmssd ;
    self.sdnn = ecgRes.sdnn;

    self.stress = ecgRes.stress; // 压力指数 0-100有效 0XFF表示无效

    self.bmr = ecgRes.bmr; // basal metabolic rate, Unit: Cards per minute(基础代谢率, 单位：卡每分钟)

    self.activeCal = ecgRes.activeCal; // Active energy consumption, Unit: Cards per minute(有功能量消耗, 单位：卡每分钟)
    [self transToConclusion:ecgRes.arrhythmia]; // 心率检查结论
}


-(NSMutableAttributedString *)symptomsPdfAttrstring {
    return nil;
}



+(NSString *)titleOfConclusion:(ECG_CONCLUSION)conclusion 
{

    NSString *string = @"";
    switch (conclusion) {
        case CONCLUSION_SINUS_RHYTHM:
        {
            string = @"Sinus Rhythm";//_L2(L_SINUS_RHYTHM);
        }
            break;
        case CONCLUSION_AFI:
        {
            string = @"Atrial Fibrillation";//_L2(L_AFI );
        }
            break;
        case CONCLUSION_HIGH_HR:
        {
            string = @"High Heart Rate";//_L2(L_H_HR );
        }
            break;
        case CONCLUSION_LOW_HR:
        {
            string = @"Low Heart Rate";//_L2( L_L_HR);
        }
            break;
        case CONCLUSION_IN_CONCLUSIVE:
        {
            string = @"Inconclusive";//_L2( L_ECG_INCONCLUSIVE);
        }
            break;
        case CONCLUSION_POOR_RECORDING:
        {
            string = @"Poor Recording";//_L2( L_POOR_RECORD);
        }
            break;
        case CONCLUSION_NOT_ENOUGH_TIME:
        {
            string = [NSString stringWithFormat:@"Measurement time is less than %d seconds", ECG_MESURING_DURA_SECS_FOR_LOCAL ];
        }
            break;
        default:
            break;
    }
    return string;
}
+(NSString *)descOfConclusion:(ECG_CONCLUSION)conclusion
{
    NSString *string = @"";
    switch (conclusion) {
        case CONCLUSION_SINUS_RHYTHM:
        {
            string = @"This ECG does not show signs of atrial fibrillation.";//_L2( L_SINUS_RHYTHM_DESC);
        }
            break;
        case CONCLUSION_AFI:
        {
            string = @"This ECG shows atrial fibrillation and you should consult your doctor immediately.";//_L2(L_AFI_DESC );
        }
            break;
        case CONCLUSION_HIGH_HR:
        {
            string = @"This ECG does not show signs of atrial fibrillation but does show a high heart rate.\r\nIf you repeatedly get this result, or you're not feeling well, you should talk to your doctor.";//_L2(L_H_HR_DESC );
        }
            break;
        case CONCLUSION_LOW_HR:
        {
            string = @"This ECG does not show signs of atrial fibrillation but does show a low heart rate.\r\nIf you repeatedly get this result, or you're not feeling well, you should talk to your doctor.";//_L2(L_L_HR_DESC );
        }
            break;
        case CONCLUSION_IN_CONCLUSIVE:
        {
            string = @"This ECG is inconclusive.\r\nIf you repeatedly get this result or you're not feeling well, you should talk to your doctor.";//_L2(L_ECG_INCONCLUSIVE_DESC );
        }
            break;
        case CONCLUSION_POOR_RECORDING:
        {
            string = @"This ECG couldn't be checked for AFib due to the quality of the recording.";//_L2(L_POOR_RECORD_DESC );
        }
            break;
        case CONCLUSION_NOT_ENOUGH_TIME:
        {
            string = [NSString stringWithFormat:@"Measurement time is less than %d seconds", ECG_MESURING_DURA_SECS_FOR_LOCAL, ECG_MESURING_DURA_SECS_FOR_LOCAL];
        }
            break;
        default:
            break;
    }
    return string;
}

-(void)transToConclusion:(CA_ECG_Arrhythmia)rhythm
{
   
    switch (rhythm) {
        case CA_Arrhythmia_NO_FINNISH:
        {
            self.conclusion = CONCLUSION_IN_CONCLUSIVE;
        }
            break;
        case CA_Arrhythmia_NORMAL:
        {
            self.conclusion = CONCLUSION_SINUS_RHYTHM;
        }
            break;
        case CA_Arrhythmia_Bradycardia:
        {
            self.conclusion = CONCLUSION_LOW_HR;
        }
            break;
        case CA_Arrhythmia_Tachycardia:
        {
            self.conclusion = CONCLUSION_HIGH_HR;
        }
            break;
        case CA_Arrhythmia_NOT_ENOUGH_DATA:
        {
            self.conclusion = CONCLUSION_POOR_RECORDING;//CONCLUSION_NOT_ENOUGH_TIME;
        }
            break;
        case CA_Arrhythmia_Abnormal_unknowReason:
        {
            self.conclusion = CONCLUSION_POOR_RECORDING;
        }
            break;
        case CA_Arrhythmia_AFib:
        {
            self.conclusion = CONCLUSION_AFI;
        }
            break;
        default:
            break;
    }
    
}
-(void)acceptFilterArray:(NSArray *)netFilterWaves
{
    NSMutableArray<NSNumber *> *checkArray = [NSMutableArray new];
    for (id obj in netFilterWaves) {
        [checkArray addObject:@([obj floatValue])];
    }
    self.volArray = checkArray;
}

-(void)analyFromNetArrhythmia:(NSDictionary *)dict
{
    // 结论
    NSDictionary *rhythmBurdens = dict[@"rhythmBurdens"];
    
    NSDictionary *bundersMap = @{  @"ATRIAL_FIBRILLATION": @(CA_Arrhythmia_AFib) , // 房颤
                                   @"BRADYCARDIA" : @(CA_Arrhythmia_Bradycardia) , // 低心率
                                   @"INCONCLUSIVE": @(CA_Arrhythmia_Abnormal_unknowReason) , // 不确定
                                   @"NORMAL": @( CA_Arrhythmia_NORMAL), // 窦性心律
                                   @"PAUSE" : @( CA_Arrhythmia_Abnormal_unknowReason) , // 不确定
                                   @"TACHYCARDIA" : @( CA_Arrhythmia_Tachycardia) , // 高心率
                                   @"UNREADABLE" : @(CA_Arrhythmia_Abnormal_unknowReason) ,// 记录不佳
    };
    
    NSArray *rhythmBurdensKeys = rhythmBurdens.allKeys;
    __block int burdensMax = 0;
    __block NSString *burdensMaxKey = nil;
    [rhythmBurdensKeys enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (burdensMax <= [rhythmBurdens[obj] intValue]){
            burdensMax = [rhythmBurdens[obj] intValue];
            burdensMaxKey = obj;
        }
    }];
    self.arrhythmia = [bundersMap[burdensMaxKey] unsignedIntegerValue];
    
    [self transToConclusion:self.arrhythmia];
    
    // 心率
    NSDictionary *heartRateTrend = dict[@"heartRateTrend"];
    self.avgHeartRate = @([heartRateTrend[@"average"] floatValue]);
    
}
-(ECG_FREQ_TYPE)frequencyType
{
    ECG_FREQ_TYPE freq = ECG_FREQ_512;
    if (!self.isRingAlgorithm) {
        // 非设备算法,采集数据使用频率 500
        freq = ECG_FREQ_500;
    }
    return freq;
}

/// 采样率
-(int)frequencyValue
{
    
    if(self.frequencyType == ECG_FREQ_512) {
        return 512;
    }
    
    if(self.frequencyType == ECG_FREQ_500) {
        return 500;
    }
    
    return 512;
    
}

-(NSMutableArray<NSNumber *> *)volArray
{
    if (!_volArray) {
        _volArray = [NSMutableArray new];
    }
    return _volArray;
}


@end
